package com.cp.support.gray;

import com.cp.support.HeaderConstant;
import com.cp.util.SceneTag;
import com.cp.util.SystemThreadLocal;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 灰度Web拦截器
 *
 * @author YangXiaoLong
 * 2022/11/3 23:44
 */
@SuppressWarnings("all")
public class GrayWebHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String gray = request.getHeader(HeaderConstant.GRAY_RELEASE);
        if (!StringUtils.isEmpty(gray)) {
            SystemThreadLocal.set(SceneTag.GRAY.getTag(), Boolean.parseBoolean(gray));
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        SystemThreadLocal.remove(SceneTag.GRAY.getTag());
    }
}
